package com.ww.yaodao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ww.yaodao.mapper.ImportProductMapper;
import com.ww.yaodao.mapper.TypeMapper;
import com.ww.yaodao.model.bo.ImportProductDO;
import com.ww.yaodao.model.bo.TypesDO;
import com.ww.yaodao.model.dto.ExcelDTO;
import com.ww.yaodao.mapper.ProductMapper;
import com.ww.yaodao.model.bo.ProductDO;
import com.ww.yaodao.model.vo.imports.ImportRespVO;
import com.ww.yaodao.model.vo.imports.ImportVO;
import com.ww.yaodao.service.FileUploadService;
import com.ww.yaodao.util.FileUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.HashMap;

@Service("fileUploadService")
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {
    @Autowired
    private Environment environment;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private ImportProductMapper importProductMapper;
    @Override
    public ImportRespVO upload(MultipartFile file , ImportVO importVO) throws Exception {
        if (file == null) {
            throw new Exception("文件不可以为空");
        }
        /*//得到上传的保存路径
        String savePath = environment.getProperty("savepath");
        //创建目录
        FileUploadUtil.mkdir(savePath);
        String dbPath = "";

        //得到上传的原文件名
        String originalFilename = file.getOriginalFilename();
        String suffix = FileUploadUtil.getFileSuffix(originalFilename);
        String filename = FileUploadUtil.getTimeFilename() + suffix;
        dbPath += filename;
        //保存
        file.transferTo(new File(savePath, filename));
        dbPath = savePath + "\\" + dbPath;*/


        // 存入订单
        ImportProductDO build = ImportProductDO.builder().eaddress(importVO.getEaddress())
                .iusername(importVO.getIusername()).itime(importVO.getItime())
                .money(importVO.getMoney()).employee(importVO.getEmployee())
                .tranceno(importVO.getTranceno()).build();
        int insert1 = importProductMapper.insert(build);

        // 存入药品
        byte[] bytes = file.getBytes();
        //调用方法进行读取
        EasyExcel.read(new ByteArrayInputStream(bytes), ExcelDTO.class, new PageReadListener<ExcelDTO>(dataList -> {
            for (ExcelDTO demoData : dataList) {
                log.info("读取到一条数据{}", JSON.toJSONString(demoData));
                Integer tid = getTid(importVO.getPtype());
                if (tid == 0) {
                    TypesDO build1 = TypesDO.builder().type(importVO.getPtype())
                            .tname(importVO.getIusername()).tdate(importVO.getItime()).build();
                    typeMapper.insert(build1);
                    tid = build1.getTid();
                    demoData.setPtype(tid);
                }
                insert(demoData);
            }
        })).sheet().doRead();

        return ImportRespVO.builder().row(insert1).build();
    }

    @Override
    public void insert(ExcelDTO excelDTO) {
        ProductDO productDO = BeanUtil.copyProperties(excelDTO, ProductDO.class);
        productMapper.insert(productDO);
    }
    public Integer getTid(String type) {
        LambdaQueryWrapper<TypesDO> typesDOQueryWrapper = new LambdaQueryWrapper<>();
        TypesDO typesDO = null;
        if (type != "") {
            typesDOQueryWrapper.like(TypesDO::getType, type);
            typesDO = typeMapper.selectOne(typesDOQueryWrapper);
            if (typesDO != null){
                return typesDO.getTid();
            }
        }
        return 0;


    }

}
